package com.ynhenc.pta.routeRank.impl;

import java.text.DecimalFormat;
import java.text.NumberFormat;

import com.ynhenc.pta.routeRank.*;

public class RouteResultImpl extends ComLib_PTA implements RouteResult {

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public RouteResultImpl(SectionList sectionList,
			RouteSearchType routeSearchType) {
		this.sectionList = sectionList;
		this.routeSearchType = routeSearchType;
		this.debug( "RouteResult(" + RouteResultImpl.ROUTE_RESULT_NO + ")");
		RouteResultImpl.ROUTE_RESULT_NO ++;
	}
	
	public double getTotalQeuryTime_Sec() {
		SectionList sectionList = this.getSectionList();
		if( sectionList == null ) {
			return 0;
		} else {
			return sectionList.getQeuryTime_Sec() + sectionList.getExtraQueryTime_Sec();
		}
	}
	
	public void addSectionList( SectionList sectionList ) {
		this.getSectionList().addList( sectionList );
	}
	
	public void addSectionList( SectionList sectionList , int pos ) {
		this.getSectionList().addList( sectionList , pos );
	}
	
	public Node getNodeStart() { 
		Section section = this.getSectionList().getStart();
		if( section == null ) {
			return null;
		} else {
			return section.getNodeStart();
		}
	}
	
	public Node getNodeEnd() {
		Section section = this.getSectionList().getEnd();
		if( section == null ) {
			return null;
		} else {
			return section.getNodeEnd();
		}
	}
	
	public String getRouteResultName() {
		return this.routeSearchType.getRouteSearchTypeName() + ", index:" + this.getIndex(); 
	}
	
	public String getDescription() {
		
		DecimalFormat nf = new DecimalFormat("#######.#####");
		//NumberFormat nf = NumberFormat.getInstance(); 
	    
		return "carbon: " + nf.format( this.getTotalCarbon_Gram()/1000.0 ) + " Kg" 
		+ ", distance: "  +  nf.format( this.getTotalLength_Meter()/1000.0 ) + " Km"
		+ ", duration: " +  nf.format( this.getTotalTime_Sec()/60.0 ) + " 분"
		+ ", query: " + nf.format( this.getTotalQeuryTime_Sec() ) + " (sec)"
		;
	}
	
	public void showDebugMessage() {
		this.debug( this.getRouteResultName() + " " + this.getDescription() );
		for( Section section : this.getSectionList() ) {
			section.showDebugMessage();
		}
	}
	
	public void toKml( StringBuffer buff ) { 
		 
		buff.append( "<Folder>" );
		buff.append( NL );
		buff.append( "<name>" );
		buff.append( this.getRouteResultName() );
		buff.append( "</name>" );
		buff.append( NL );
		buff.append( "<visibility>" );
		buff.append( this.getVisibility() ); 
		buff.append( "</visibility>" );
		buff.append( NL );
		buff.append( "<open>0</open>" ); 
		buff.append( NL );
		buff.append( "<description>" ); 
		buff.append( this.getDescription() ); 
		buff.append( "</description>" ); 
		buff.append( NL );  
		
		 for( Section section : this.getSectionList() ) {
			 section.toKml( buff );
			 buff.append( NL );
		 }
		 
		 buff.append( "</Folder>" );  
	}

	public void setRoutResult(RouteSearchType routResultType) {
		this.routeSearchType = routResultType;
	}

	public int getCountSection() {
		return this.sectionList.size();
	}

	public Section getSection(int index) {
		return this.sectionList.get(index);
	}

	public RouteSearchType getRouteSearchType() {
		return this.routeSearchType;
	}
	
	public void setRouteSearchType( RouteSearchType routeSearchType ) {
		this.routeSearchType = routeSearchType;
	}

	public SectionList getSectionList() {
		return this.sectionList;
	}
	
	public double getTotalLength_Meter() {
		double sum = 0;
		for( Section section : this.getSectionList() ) {
			sum += section.getLength_Meter();
		}
		return sum;
	} 
	
	public double getTotalTime_Sec() { 
		double sum = 0;
		for( Section section : this.getSectionList() ) {
			sum += section.getTime_Sec();
		}
		return sum;
	}
	
	public double getTotalCarbon_Gram() {
		double sum = 0;
		for( Section section : this.getSectionList() ) {
			sum += section.getCarbon_Gram();
		}
		return sum;
	} 
	
	public int getVisibility() {
		return this.visiblity;
	}
	
	private RouteSearchType routeSearchType;
	private int index;
	private int visiblity = 1;
	private SectionList sectionList = new SectionList(); 
	
	private static int ROUTE_RESULT_NO = 0;

}
